package leagal_aid.util;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {

	static final String DB_URL = "jdbc:mysql://localhost:3306/flyz?useUnicode=true&characterEncoding=utf8";
	// MySQL的JDBC URL编写方式：jdbc:mysql://主机名称：连接端口/数据库的名称
	static final String USER = "root";
	static final String PASS = "";
	public Connection conn = null;

	//加载驱动，不同的数据库，驱动不一样,这里是mysql
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	//连接数据库
	public Connection geConnection() {
		try {
			//如果连接为空，就连接数据库
			if (conn == null) 
				conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);
		} catch (SQLException e) {
			//捕获异常
			e.printStackTrace();
		}
		return conn;
	}

	//关闭数据库
	public void close() {
		try {			
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			e.printStackTrace(new PrintWriter(System.out));
		}
	}


}
